package com.ming.elm.controller;

import com.ming.elm.po.DeliveryAddress;
import com.ming.elm.service.DeliveryAddressService;
import com.ming.elm.service.impl.DeliveryAddressServiceImpl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

public class DeliveryAddressController {
	private DeliveryAddressService das = new DeliveryAddressServiceImpl();

	public Object listDeliveryAddressByUserId(HttpServletRequest request) throws Exception{
		String userId = request.getParameter("userId");
		List<DeliveryAddress> list = das.listDeliveryAddressByUserId(userId);

		return list;
	}
	
	public Object saveDeliveryAddress(HttpServletRequest request) throws Exception{
		String contactName = request.getParameter("contactName");
		String str1 = request.getParameter("contactSex");
		String contactTel = request.getParameter("contactTel");
		String address = request.getParameter("address");
		String userId = request.getParameter("userId");
		Integer contactSex = Integer.valueOf(str1);

		System.out.println(contactName);
		System.out.println(address);

		DeliveryAddress da = new DeliveryAddress();
		da.setContactName(contactName);
		da.setContactSex(contactSex);
		da.setContactTel(contactTel);
		da.setAddress(address);
		da.setUserId(userId);

		int result = das.saveDeliveryAddress(da);

		return result;
	}
	
	public Object getDeliveryAddressById(HttpServletRequest request) throws Exception{
		String str = request.getParameter("daId");
		Integer daId = Integer.valueOf(str);
		DeliveryAddress da = das.getDeliveryAddressById(daId);

		return da;
	}
	
	public Object updateDeliveryAddress(HttpServletRequest request) throws Exception{
		String contactName = request.getParameter("contactName");
		String str = request.getParameter("contactSex");
		String contactTel = request.getParameter("contactTel");
		String address = request.getParameter("address");
		String str2 = request.getParameter("daId");
		Integer contactSex = str!=null ? Integer.valueOf(str) : null;
		Integer daId = Integer.valueOf(str2);

		DeliveryAddress da = new DeliveryAddress();
		da.setContactName(contactName);
		da.setAddress(address);
		da.setContactSex(contactSex);
		da.setContactTel(contactTel);
		da.setDaId(daId);

		int result = das.updateDeliveryAddress(da);

		return result;
	}
	
	public Object removeDeliveryAddress(HttpServletRequest request) throws Exception{
		String str = request.getParameter("daId");
		Integer daId = Integer.valueOf(str);

		int result = das.removeDeliveryAddress(daId);

		return result;
	}
}
